<template>
  <div class="login-container">
    <el-card class="login-card" shadow="hover">
      <h2 class="title">管理员登录</h2>
      <el-form
        :model="loginForm"
        @submit.native.prevent="handleLogin"
        class="login-form"
        label-width="70px"
      >
        <el-form-item label="用户名" prop="account">
          <el-input v-model="loginForm.account" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit" class="login-btn"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { login } from "@/api/login";
import { setToken } from "@/tools/cookieMgr";
import loading from "@/tools/loading";
import { throttle, toast } from "@/tools/tools";
import { ref } from "vue";
import { useRouter } from "vue-router";

const router = useRouter();

const loginForm = ref({
  account: "admin",
  password: "123456",
});

const handleLogin = throttle(async () => {
  // 登录逻辑，这里可以添加 API 请求等
  const res = await login(loginForm.value);
  if (res.code >= 0) {
    // 设置token到cookie
    setToken(res.data.token);
    toast.success(res.message);
    router.push("/");
    loading.show("正在进入首页...");
  } else {
    toast.error(res.message);
  }
});
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #72c6ef, #d3c4e3);
}

.login-card {
  width: 400px;
  padding: 20px;
}

.title {
  text-align: center;
  margin-bottom: 20px;
}

.login-form {
  max-width: 100%;
}

.login-btn {
  width: 100%;
}

.el-form-item {
  margin-bottom: 20px;
}
</style>
